package com.ruoyi.framework.interceptor.impl;

import com.ruoyi.common.annotation.ControllerAuthority;
import com.ruoyi.common.core.domain.entity.Team;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.interceptor.ControllerAuthorityInterceptor;
import com.ruoyi.system.service.ISysTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author : hcl
 * @Date : 2023/8/8 19:09:24
 */
@Component
public class QywxControllerInterceptor extends ControllerAuthorityInterceptor {
    private ISysTeamService teamService;

    @Autowired
    public void setTeamService(ISysTeamService teamService) {
        this.teamService = teamService;
    }

    @Override
    public boolean isPermission(HttpServletRequest request, ControllerAuthority annotation) {
        String tmid = request.getHeader("Tmid");
        if (Objects.nonNull(tmid)) {
            Team team = teamService.selectTeamById(Long.parseLong(tmid),
                    SecurityUtils.getLoginUser().getUserId());
            if (team != null) {
                request.setAttribute("team", team);
                return !team.isLock() && annotation.value().getCode() == team.getTeamType();
            }
        }
        return false;
    }
}
